import wx
from wx.lib.wordwrap import wordwrap
import os
import string

TOOL_NAME_STRING="NISSAN DCM Release Tool"

ID_CHECK_BOX_RELEASE = wx.NewId()
ID_CHECK_BOX_UPDATE_VIEW = wx.NewId()
ID_CHECK_BOX_UPDATE_VERSION_FILE = wx.NewId()
ID_CHECK_BOX_GENERATE_CODE = wx.NewId()
ID_CHECK_BOX_COMPILE_CODE = wx.NewId()
ID_CHECK_BOX_SAVE_IMAGE = wx.NewId()
ID_TXT_BOX_VER_HIGH = wx.NewId()
ID_TXT_BOX_VER_LOW = wx.NewId()
ID_TXT_BOX_VER_TYPE = wx.NewId()
ID_STATIC_TEXT_SGOLD_VER = wx.NewId()

FILTER_TYPE=1
FILTER_DIGIT=2

class CActionPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent, -1)

        self.dimension=3

        self.cbList = []

        self.CreateCheckBox()

        self.box = wx.StaticBox(self, -1, "Please select action")

        self.LayOut()

    def OnChkRelease(self, event):
        if event.IsChecked():
            value = wx.CHK_UNDETERMINED
        else:
            value = wx.CHK_UNCHECKED

        for cb in self.cbList:
            cb.Set3StateValue(value)

    def OnChkAction(self, event):
        if event.IsChecked() == False:
            self.cbRelease.SetValue(False)

        for cb in self.cbList:
            if cb.Get3StateValue() != wx.CHK_UNCHECKED:
                cb.Set3StateValue(wx.CHK_CHECKED)

    def CreateCheckBox(self):
        self.cbRelease = wx.CheckBox(self, ID_CHECK_BOX_RELEASE, "Release",size=(150,20))
        self.cbUpdateView = wx.CheckBox(self, ID_CHECK_BOX_UPDATE_VIEW, "Update View",size=(10,20),style=wx.CHK_3STATE)
        self.cbUpdateVF = wx.CheckBox(self, ID_CHECK_BOX_UPDATE_VERSION_FILE, "Update Version File",size=(10,20),style=wx.CHK_3STATE)
        self.cbGenerate = wx.CheckBox(self, ID_CHECK_BOX_GENERATE_CODE, "Generate Code",size=(10,20),style=wx.CHK_3STATE)
        self.cbCompile = wx.CheckBox(self, ID_CHECK_BOX_GENERATE_CODE, "Compile Code",size=(10,10),style=wx.CHK_3STATE)
        self.cbSave = wx.CheckBox(self, ID_CHECK_BOX_SAVE_IMAGE, "Save Image",size=(120,10),style=wx.CHK_3STATE)

        self.Bind(wx.EVT_CHECKBOX, self.OnChkRelease, self.cbRelease)
        self.Bind(wx.EVT_CHECKBOX, self.OnChkAction, self.cbUpdateView)
        self.Bind(wx.EVT_CHECKBOX, self.OnChkAction, self.cbUpdateVF)
        self.Bind(wx.EVT_CHECKBOX, self.OnChkAction, self.cbGenerate)
        self.Bind(wx.EVT_CHECKBOX, self.OnChkAction, self.cbCompile)
        self.Bind(wx.EVT_CHECKBOX, self.OnChkAction, self.cbSave)

        #self.cbTopList.append(self.cbRelease)

        self.cbList.append(self.cbUpdateView)
        self.cbList.append(self.cbUpdateVF)
        self.cbList.append(self.cbGenerate)
        self.cbList.append(self.cbCompile)
        self.cbList.append(self.cbSave)

    def LayOut(self):

        #the main sizer for the this static box
        self.sizer = wx.StaticBoxSizer(self.box, wx.VERTICAL)

        #the vetical 
        self.vSizer = wx.BoxSizer(wx.VERTICAL)

        #the sizer list for the action
        #every sizer include 3 item
        sizerList = []
        ix=0
        for cb in self.cbList:
            if ix == 0:
                hSizer = wx.BoxSizer(wx.HORIZONTAL)
                sizerList.append(hSizer)

            hSizer.Add(cb, 1, wx.EXPAND)

            ix = (ix+1)%self.dimension

        if ix != 0 and ix < self.dimension:
            for i in range(ix,self.dimension):
                hSizer.Add((1,0), 1, wx.EXPAND)



        #the release check box is at the top
        self.vSizer.Add(self.cbRelease, 0, wx.EXPAND|wx.ALL, 5)

        for sizer in sizerList:
            self.vSizer.Add(sizer, 1, wx.EXPAND | wx.LEFT, 5)

        self.sizer.Add(self.vSizer, 1, wx.EXPAND)

        self.SetSizerAndFit(self.sizer)


class CVerValidator(wx.PyValidator):
    def __init__(self, flag=None):
        wx.PyValidator.__init__(self)
        self.Bind(wx.EVT_CHAR, self.OnChar)
        self.typeList = ['R','I','D','r','i','d']

        self.flag=flag

    def Clone(self):
        return CVerValidator(self.flag)

    def OnChar(self, event):
        key = event.GetKeyCode()

        if key == wx.WXK_BACK or key == wx.WXK_DELETE:
            event.Skip()
            return

        if self.flag == FILTER_TYPE:
            if chr(key) not in self.typeList:
                return
            else:
                self.GetWindow().SetValue(chr(key).upper())
                return
        elif self.flag == FILTER_DIGIT:
            if chr(key) not in string.digits:
                return
        event.Skip()
        return

    def Validate(self, win):
        val = self.GetWindow().GetValue()

        if self.flag == FILTER_TYPE:
            if val not in self.typeList:
                return False
        elif self.flag == FILTER_DIGIT:
            for x in val:
                if x not in string.digits:
                    return False

        return True

class CVersionPanel(wx.Panel):
    '''Panel for version section'''
    def __init__(self, parent):
        wx.Panel.__init__(self, parent, -1)

        font = wx.Font(10, wx.SWISS, wx.NORMAL, wx.BOLD)

        #================
        #version high
        self.tbSgVerHigh = wx.TextCtrl(self, ID_TXT_BOX_VER_HIGH, size=(30,22), style=wx.ALIGN_RIGHT, validator=CVerValidator(FILTER_DIGIT))
        self.tbSgVerHigh.SetFont(font)

        #version low
        self.tbSgVerLow = wx.TextCtrl(self, ID_TXT_BOX_VER_LOW, size=(30,22), style=wx.ALIGN_RIGHT, validator=CVerValidator(FILTER_DIGIT))
        self.tbSgVerLow.SetFont(font)

        #version type
        #self.tbSgVerType = wx.TextCtrl(self, ID_TXT_BOX_VER_TYPE, size=(22,22), style=wx.ALIGN_RIGHT, validator=MyValidator(ALPHA_ONLY))
        self.tbSgVerType = wx.TextCtrl(self, ID_TXT_BOX_VER_TYPE, size=(22,22), style=wx.ALIGN_RIGHT, validator=CVerValidator(FILTER_TYPE))
        self.tbSgVerType.SetFont(font)

        #seperator
        self.stPoint = wx.StaticText(self, ID_STATIC_TEXT_SGOLD_VER, ".")
        self.stPoint.SetFont(font)

        #sgold version
        self.stSgVersion = wx.StaticText(self, ID_STATIC_TEXT_SGOLD_VER, r"Sgold Version   ", style=wx.ALIGN_CENTER, size=(80, -1))
        verFont = wx.Font(10, wx.SWISS, wx.NORMAL, wx.BOLD)
        #self.stSgVersion.SetFont(verFont)

        #sgold version example
        self.stSgExample = wx.StaticText(self, -1, r"eg:  02.08R", style=wx.ALIGN_CENTER, size=(80, -1))


        #================
        #sgold SVN
        self.stSgSVN = wx.StaticText(self, -1, r"Sgold SVN   ", style=wx.ALIGN_CENTER, size=(80, -1))
        self.tbDefault1 = wx.TextCtrl(self, -1, "00", size=(22,22))
        self.tbDefault1.SetFont(font)
        self.tbDefault1.Disable()
        self.tbDefault2 = wx.TextCtrl(self, -1, "00", size=(22,22), style=wx.ALIGN_RIGHT)
        self.tbDefault2.SetFont(font)
        self.tbDefault2.Disable()
        self.tbSvn1 = wx.TextCtrl(self, -1, size=(22,22), style=wx.ALIGN_RIGHT, validator=CVerValidator(FILTER_DIGIT))
        self.tbSvn1.SetFont(font)
        self.tbSvn2 = wx.TextCtrl(self, -1, size=(22,22), style=wx.ALIGN_RIGHT, validator=CVerValidator(FILTER_DIGIT))
        self.tbSvn2.SetFont(font)
        self.tbDefault3 = wx.TextCtrl(self, -1, "06", size=(22,22), style=wx.ALIGN_RIGHT)
        self.tbDefault3.SetFont(font)
        self.tbDefault3.Disable()
        self.tbDefault4 = wx.TextCtrl(self, -1, "01", size=(22,22), style=wx.ALIGN_RIGHT)
        self.tbDefault4.SetFont(font)
        self.tbDefault4.Disable()
        self.stPoint1 = wx.StaticText(self, -1, ".")
        self.stPoint1.SetFont(font)
        self.stPoint2 = wx.StaticText(self, -1, ".")
        self.stPoint2.SetFont(font)
        self.stPoint3 = wx.StaticText(self, -1, ".")
        self.stPoint3.SetFont(font)
        self.stSemiCol1 = wx.StaticText(self, -1, ":")
        self.stSemiCol1.SetFont(font)
        self.stSemiCol2 = wx.StaticText(self, -1, ":")
        self.stSemiCol2.SetFont(font)

        #================
        #s12 version
        self.stS12 = wx.StaticText(self, -1, r"S12 Version  ", style=wx.ALIGN_CENTER, size=(80, -1))
        self.tbS12Ver = wx.TextCtrl(self, -1, size=(150,22), style=wx.ALIGN_LEFT)
        self.tbS12Ver.SetFont(font)
        #sgold version example
        self.stS12Example = wx.StaticText(self, -1, r"eg:  2.0.4_20120402", style=wx.ALIGN_CENTER, size=(80, -1))

        self.LayOut()

    def LayOut(self):

        self.sizer = wx.BoxSizer(wx.VERTICAL)

        #sizer for SGOLD version
        self.sgSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.sgSizer.Add(self.stSgVersion, 0, wx.EXPAND | wx.TOP, 3)
        self.sgSizer.Add(self.tbSgVerHigh, 0, wx.EXPAND | wx.RIGHT, 3)
        self.sgSizer.Add(self.stPoint, 0, wx.EXPAND | wx.TOP, 6)
        self.sgSizer.Add(self.tbSgVerLow, 0, wx.EXPAND | wx.LEFT | wx.RIGHT, 3)
        self.sgSizer.Add(self.tbSgVerType, 0, wx.EXPAND| wx.LEFT, 3)

        #sizer for SGOLD SVN
        #the format is 00.00:XX.XX:06.01
        self.svnSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.svnSizer.Add(self.stSgSVN, 0, wx.EXPAND | wx.TOP, 3)
        self.svnSizer.Add(self.tbDefault1, 0, wx.EXPAND | wx.RIGHT, 3)
        self.svnSizer.Add(self.stPoint1, 0, wx.EXPAND | wx.TOP, 6)
        self.svnSizer.Add(self.tbDefault2, 0, wx.EXPAND | wx.RIGHT | wx.LEFT, 3)

        self.svnSizer.Add(self.stSemiCol1, 0, wx.EXPAND | wx.TOP, 3)

        self.svnSizer.Add(self.tbSvn1, 0, wx.EXPAND | wx.RIGHT | wx.LEFT, 3)
        self.svnSizer.Add(self.stPoint2, 0, wx.EXPAND | wx.TOP, 6)
        self.svnSizer.Add(self.tbSvn2, 0, wx.EXPAND | wx.RIGHT | wx.LEFT, 3)

        self.svnSizer.Add(self.stSemiCol2, 0, wx.EXPAND | wx.TOP, 3)

        self.svnSizer.Add(self.tbDefault3, 0, wx.EXPAND | wx.RIGHT | wx.LEFT, 3)
        self.svnSizer.Add(self.stPoint3, 0, wx.EXPAND | wx.TOP, 6)
        self.svnSizer.Add(self.tbDefault4, 0, wx.EXPAND | wx.LEFT, 3)



        #sizer for S12 version
        self.s12Sizer = wx.BoxSizer(wx.HORIZONTAL)
        self.s12Sizer.Add(self.stS12, 0, wx.EXPAND | wx.TOP, 3)
        self.s12Sizer.Add(self.tbS12Ver, 1, wx.EXPAND | wx.RIGHT, 3)

        self.verSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.verSizer.Add(self.sgSizer, 1, wx.EXPAND | wx.TOP | wx.BOTTOM, 5)
        self.verSizer.Add(self.s12Sizer, 1, wx.EXPAND | wx.TOP | wx.BOTTOM, 5)

        #sizer for example
        self.exSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.exSizer.Add(self.stSgExample, 1, wx.EXPAND)
        self.exSizer.Add((50,0), 0, wx.EXPAND)
        self.exSizer.Add(self.stS12Example, 1, wx.EXPAND)

        self.sizer.Add(self.verSizer, 1, wx.EXPAND | wx.TOP , 5)
        self.sizer.Add(self.exSizer, 0, wx.EXPAND| wx.BOTTOM, 5)
        self.sizer.Add(self.svnSizer, 0, wx.EXPAND | wx.TOP | wx.BOTTOM, 5)
        self.sizer.Add((0,1), 0, wx.EXPAND)

        self.SetSizerAndFit(self.sizer)

class CInfoPanel(wx.Panel):
    '''The infomation panel'''
    def __init__(self, parent):
        wx.Panel.__init__(self, parent, -1)

        self.box = wx.StaticBox(self, -1, "Please fill release information")

        #the view path 
        self.stViewPath = wx.StaticText(self, -1, "View   ",style=wx.ALIGN_CENTER, size=(80,-1))
        self.pathCtrl = wx.TextCtrl(self, -1,size=(250,-1), style=wx.TE_READONLY)
        #self.pathCtrl.Disable()
        self.browseBtn = wx.Button(self, -1, "Browse", size=(50,-1))
        self.Bind(wx.EVT_BUTTON, self.OnBrowse, self.browseBtn)


        #the sgold version
        self.verPanel = CVersionPanel(self)

        self.LayOut()

    def LayOut(self):
        V_SCAPE = 5
        H_SCAPE = 10

        self.viewSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.viewSizer.Add(self.stViewPath, 0, wx.EXPAND | wx.TOP, 3)
        self.viewSizer.Add(self.pathCtrl, 1, wx.EXPAND)
        self.viewSizer.Add((H_SCAPE,0), 0, wx.SHAPED)
        self.viewSizer.Add(self.browseBtn, 0, wx.SHAPED)

        self.verSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.verSizer.Add(self.verPanel, 1, wx.EXPAND)



        self.sizer = wx.StaticBoxSizer(self.box, wx.VERTICAL)
        self.sizer.Add((0,V_SCAPE), 0, wx.EXPAND)
        self.sizer.Add(self.viewSizer, 0, wx.EXPAND)
        self.sizer.Add((0,V_SCAPE), 0, wx.EXPAND)
        self.sizer.Add(self.verSizer, 1, wx.EXPAND)
        self.SetSizerAndFit(self.sizer)

    def OnBrowse(self, event):
        dlg = wx.DirDialog(self, "Choose View Directory:",
                          style=wx.DD_DEFAULT_STYLE
                           #| wx.DD_DIR_MUST_EXIST
                           #| wx.DD_CHANGE_DIR
                           )
        if dlg.ShowModal() == wx.ID_OK:
            self.pathCtrl.SetValue(dlg.GetPath())

class CCarTypePanel(wx.Panel):
    '''This Panel used to select car type'''
    def __init__(self, parent):
        wx.Panel.__init__(self, parent, -1)

        carTypeList = ['DFL', 'NCIC', 'L42L', 'EV', 'INFINITI']
        self.rbCarType = wx.RadioBox(self, -1, "Choose the car type", pos=(-1,10), choices=carTypeList, style = wx.RA_SPECIFY_COLS, majorDimension=3)

class CLogoPanel(wx.Panel):
    '''The panel for Logo'''
    def __init__(self, parent):
        wx.Panel.__init__(self, parent, -1)
        image = wx.Image(('pic/logo.png'),wx.BITMAP_TYPE_PNG).ConvertToBitmap()
        self.logo = wx.StaticBitmap(self, -1, image, (-1, -1), (image.GetWidth(), image.GetHeight()))
        image = wx.Image(('pic/logo1.jpg'),wx.BITMAP_TYPE_JPEG).ConvertToBitmap()
        self.logo1 = wx.StaticBitmap(self, -1, image, (-1, -1), (image.GetWidth(), image.GetHeight()))

        self.LayOut()
    def LayOut(self):
        self.rSizer = wx.BoxSizer(wx.VERTICAL)

        self.rSizer.Add(self.logo1, 1, wx.ALIGN_RIGHT)

        self.sizer = wx.BoxSizer(wx.HORIZONTAL)
        self.sizer.Add(self.logo, 0, wx.SHAPED|wx.ALIGN_LEFT|wx.ALIGN_CENTER_VERTICAL)
        self.sizer.Add(self.rSizer, 1, wx.EXPAND)

        self.SetSizerAndFit(self.sizer)


class CReleasePanel(wx.Panel):
    '''the panel for release'''
    def __init__(self, parent):
        wx.Panel.__init__(self, parent, -1)

        #self.btnRun = wx.Button(self, label="Run")
        #self.btnStop = wx.Button(self, label="Stop")



        self.actionPanel = CActionPanel(self)
        #self.verPanel = CVersionPanel(self)
        self.carTypePanel = CCarTypePanel(self)
        self.infoPanel = CInfoPanel(self)
        self.logoPanel = CLogoPanel(self)

        self.LayOut()


    def LayOut(self):
        #grid sizer
        #self.lgSizer = wx.GridBagSizer(hgap=5, vgap=5)
        #self.lgSizer.Add(self.btnStop, pos=(0,0))
        #self.lgSizer.Add(self.actionPanel, pos=(10,0))


        #left sizer
        #self.lSizer = wx.BoxSizer(wx.VERTICAL)
        #self.lSizer.Add(self.lgSizer, 1, wx.EXPAND)
        #self.lSizer.Add(self.btnRun, 0, wx.CENTER)


        #right sizer
        self.rSizer = wx.BoxSizer(wx.VERTICAL)
        self.rSizer.Add(self.logoPanel, 0, wx.EXPAND)
        #self.rSizer.Add(self.verPanel, 0, wx.ALIGN_LEFT)
        self.rSizer.Add(self.carTypePanel, 0, wx.EXPAND | wx.TOP | wx.LEFT | wx.RIGHT, 10)
        self.rSizer.Add(self.actionPanel, 0, wx.EXPAND | wx.TOP | wx.LEFT | wx.RIGHT, 10)
        self.rSizer.Add(self.infoPanel, 0, wx.EXPAND | wx.TOP | wx.LEFT | wx.RIGHT, 10)

        self.sizer = wx.BoxSizer(wx.HORIZONTAL)
        #self.sizer.Add(self.lSizer, 1, wx.EXPAND)
        self.sizer.Add(self.rSizer, 1, wx.EXPAND)

        self.SetSizer(self.sizer)
        self.SetAutoLayout(1)
        self.sizer.Fit(self)




class CReleaseFrame(wx.Frame):
    '''the Frame for Release'''
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, title=title, size=(500,700), style=wx.MINIMIZE_BOX | wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX | wx.CLIP_CHILDREN)


        #the frame has a status bar
        staBar = self.CreateStatusBar()
        menuBar = self.CreateMenuBar()
        self.CreateIcon()

        panel = CReleasePanel(self)


        box = wx.BoxSizer(wx.VERTICAL)
        box.Add(wx.StaticLine(self), 0, wx.EXPAND)
        box.Add(panel, 1, wx.EXPAND)
        self.SetSizer(box)
        self.Show()

    def CreateIcon(self):
        image = wx.Image(('pic/icon.jpg'),wx.BITMAP_TYPE_JPEG).ConvertToBitmap()
        self.icon = wx.IconFromBitmap(image)
        self.SetIcon(self.icon)

    def CreateMenuBar(self):
        mb = wx.MenuBar()
        #create  the File menu
        fileMenu = wx.Menu()
        mOpen = fileMenu.Append(wx.ID_OPEN, "&Open")
        self.Bind(wx.EVT_MENU, self.OnOpen, mOpen)

        fileMenu.AppendSeparator()

        mExit = fileMenu.Append(wx.ID_EXIT, "E&xit")
        self.Bind(wx.EVT_MENU, self.OnExit, mExit)


        #create the help menu
        helpMenu = wx.Menu()
        mHelp = helpMenu.Append(wx.ID_HELP, "&Help Topics")
        self.Bind(wx.EVT_MENU, self.OnHelp, mHelp)

        helpMenu.AppendSeparator()

        mAbout = helpMenu.Append(wx.ID_ABOUT, "&About")
        self.Bind(wx.EVT_MENU, self.OnAbout, mAbout)


        mb.Append(fileMenu, "&File")
        mb.Append(helpMenu, "&Help")
        self.SetMenuBar(mb)

        return mb

    def OnOpen(self, event):
        pass
    def OnExit(self, event):
        self.Destroy()
        event.Skip()

    def OnHelp(self, event):
        msg = "This Is the Help Dialog of %s.\n\n" % TOOL_NAME_STRING

        dlg = wx.MessageDialog(self, msg, TOOL_NAME_STRING,
                               wx.OK | wx.ICON_INFORMATION)
        dlg.ShowModal()
        dlg.Destroy()
        pass

    def OnAbout(self, event):
        info = wx.AboutDialogInfo()

        info.Name = TOOL_NAME_STRING

        info.Version = "0.1.0"
        info.Copyright = "(C) 2012 Programmers and Coders Everywhere"
        #info.Description = wordwrap(
        #    "\"NISSAN DCM Release Tool\" program is a software program that Release the NISSAN DCM Image",
        #    700, wx.ClientDC(self))
        info.Developers = [ "Zengj"]

        #licenseText = "No Liense for this Tool"
        #info.License = wordwrap(licenseText, 500, wx.ClientDC(self))

        # Then we call wx.AboutBox giving it that info object
        wx.AboutBox(info)




if __name__ == "__main__":
    app = wx.App(False)
    frame = CReleaseFrame(None, TOOL_NAME_STRING)
    app.MainLoop()
